/*! ******************************************************************************
 *
 * Created on 2021年11月30日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.pentaho.di.ui.spoon.navigation.view.category;

import org.eclipse.swt.widgets.Tree;
import org.pentaho.di.core.util.Utils;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.ui.core.widget.TreeMemory;
import org.pentaho.di.ui.spoon.Spoon;
import org.pentaho.di.ui.spoon.TabMapEntry;
import org.pentaho.di.ui.spoon.navigation.view.CategoryTree;
import org.pentaho.di.ui.spoon.navigation.view.MetaTree;
import org.pentaho.di.ui.spoon.navigation.view.item.TransMetaTree;

/**
 * 主对象转换类
 * 
 * @author hs@yzbdl.ac.cn
 *
 */
public class TransCategory extends CategoryTree {

	/**
	 * @param parent
	 */
	public TransCategory(Spoon spoon, Tree parent) {
		super(spoon, parent);
	}

	/**
	 * 
	 * (non-Javadoc)
	 * 
	 * @see org.pentaho.di.ui.spoon.navigation.view.CategoryTree#getName()
	 */
	@Override
	public String getName() {
		return Spoon.STRING_TRANSFORMATIONS;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see org.pentaho.di.ui.spoon.navigation.view.CategoryTree#buildMetaTree()
	 */
	@Override
	public MetaTree buildMetaTree() {
		// 获取已打开的转换
		TransMeta activeTransMeta = this.getSpoonRef().getActiveTransformation();

		// 循环右侧的Tabs
		for (TabMapEntry entry : this.getSpoonRef().getDelegates().tabs.getTabs()) {
			Object managedObject = entry.getObject().getManagedObject();
			if (managedObject instanceof TransMeta) {
				TransMeta transMeta = (TransMeta) managedObject;

				/*
				 * 1非仅显示激活转换/作业, 2当前打开转换不为空,切和当前循环转换是同一个对象.
				 */
				if (!this.getSpoonRef().isOnlyActiveFileShownInTree()
						|| (activeTransMeta != null && activeTransMeta.equals(transMeta))) {
					// 根据打开的转换Tab，新增一个Tree item.
					String name = this.getSpoonRef().getDelegates().tabs.makeTabName(transMeta,
							entry.isShowingLocation());
					if (Utils.isEmpty(name)) {
						name = Spoon.STRING_TRANS_NO_NAME;
					}
					TransMetaTree transMetaTree = new TransMetaTree(this, name);

					// 如果仅显示处于激活状态的文件，那么默认展示树节点
					if (this.getSpoonRef().isOnlyActiveFileShownInTree()) {
						TreeMemory.getInstance().storeExpanded(Spoon.STRING_SPOON_MAIN_TREE, transMetaTree.getView(),
								true);
					}

					transMetaTree.buildSubTree(transMeta);
				}
			}
		}
		return null;
	}

}
